﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using neut.Properties;
using System.Deployment.Application;

namespace neut
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        //[PreEmptive.Attributes.Setup(CustomEndpoint = "so-s.info/PreEmptive.Web.Services.Messaging/MessagingServiceV2.asmx")]
        //[PreEmptive.Attributes.Teardown()]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (!ApplicationDeployment.IsNetworkDeployed)
                {
                    var thread = new Thread(new ThreadStart(() =>
                    {
                        CheckVersion();
                    }));
                    thread.Start();
                }
 
                
                var thread2 = new Thread(new ThreadStart(() =>
                {
                    if (Settings.Default.FistTimeUse)
                    {
                        MessageBox.Show("本程序开发与测试环境为NoteExpress 2.8.1.2024！", "使用说明", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                        Settings.Default.FistTimeUse = false;
                        Settings.Default.Save();
                    }
                }));
                thread2.Start();

                var main = new Form1();
                Application.Run(main);
            }
            catch (System.Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        static string GetVersion()
        {
            var RssUrl = @"http://neut.codeplex.com/Project/ProjectRss.aspx?ProjectRSSFeed=codeplex%3a%2f%2frelease%2fNEUT";
            var regex = @".*NoteExpress User Tools (\d+\.\d+\.\d+).*";
            string version = "";
            XmlDocument doc = new XmlDocument();

            doc.Load(RssUrl);
            XmlNodeList nodelist = doc.GetElementsByTagName("item");
            if (doc.HasChildNodes)
            {
                foreach (XmlNode node in nodelist)
                {
                    if (node.HasChildNodes)
                    {
                        var objItems1 = node.ChildNodes;
                        foreach (XmlNode node1 in objItems1)
                        {
                            if (node1.Name == "title")
                            {
                                version = node1.InnerText;
                                break;
                            }
                        }
                    }
                    break;
                }
            }
            var reg = System.Text.RegularExpressions.Regex.Match(version, regex);
            return reg.Groups[1].Value;
            //return "1.9.6";
        }

        static void CheckVersion()
        {
            try
            {
                var AssemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                var newVersion = new Version(GetVersion() + "." + AssemblyVersion.Revision);
                if (AssemblyVersion < newVersion)
                {
                    if (Settings.Default.UpdateNotifyDate != string.Empty)
                    {
                        var date = DateTime.Parse(Settings.Default.UpdateNotifyDate);
                        if (date.AddDays(3) > DateTime.Now)
                            return;
                    }

                    if (MessageBox.Show("有新版本，是否前往更新？", "更新通知", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
                        == DialogResult.Yes)
                        System.Diagnostics.Process.Start("http://neut.codeplex.com/Release/ProjectReleases.aspx");
                    Settings.Default.UpdateNotifyDate = DateTime.Now.ToString();
                    Settings.Default.Save();
                }
            }
            catch (System.Exception)
            {

            }
        }
    }
}
